/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io;

import org.openaecfm.spf.model.HeaderEntity;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.StringParameter;
import org.openaecfm.spf.model.FileSchema;

/**
 * Input and output FileSchema.
 * @author Qiu Kuining
 */
public final class FileSchemaIO {

    /**
     * Export FileSchema.
     * @param pojo
     * @return
     */
    public HeaderEntity exportSPF(FileSchema pojo) {
        HeaderEntity he = new HeaderEntity("FILE_SCHEMA");

        // Set Schema_identifiers
        ListParameter l = new ListParameter();
        for (String s : pojo.getSchema_identifiers()) {
            l.add(new StringParameter(s, StringParameter.UTF_8));
        }
        he.addParameter(l);

        return he;
    }

    /**
     * Import FileSchema.
     * @param he
     * @return
     */
    public FileSchema importSPF(HeaderEntity he) {
        FileSchema pojo = new FileSchema();

        // Set Schema_identifiers
        ListParameter l = (ListParameter) he.getParameter(0);
        for (Parameter p : l) {
            StringParameter s = (StringParameter) p;
            pojo.addSchema_identify(s.toUTF8());
        }

        return pojo;
    }
}
